package com.devunion.reader.server.service;

import org.gwtwidgets.server.spring.GWTSpringController;
import com.devunion.reader.client.service.UserService;
import com.devunion.reader.client.to.UserManagementTo;
import com.devunion.bogdan.model.project.dao.UserDao;
import com.devunion.bogdan.model.project.User;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;

import java.util.List;
import java.util.ArrayList;

/**
 * @author Timoshenko Alexander 19.08.2009 : 14:40:14
 */
public class UserServiceImpl extends GWTSpringController implements UserService {
    private UserDao userDao;

    public ListLoadResult<UserManagementTo> getUsers() {
        List<UserManagementTo> result = new ArrayList<UserManagementTo>();
        List<User> users = userDao.loadAll();
        if (users != null && !users.isEmpty()) {
            for (User user : users) {
                UserManagementTo to = new UserManagementTo();
                to.setId(user.getUserId());
                to.setUsername(user.getLogin());
                to.setRole(user.getRole().name());
                result.add(to);
            }
        }
        return new BaseListLoadResult<UserManagementTo>(result);
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
